using System;
using System.Collections.Generic;
using System.Text;
using MCromwell.BDD;
using MCromwell.StaffIntranet.Task.Infrastructure.Logging;
using MCromwell.StaffIntranet.DependencyInjection;
using Rhino.Mocks;
using Mapper;
using MCromwell.StaffIntranet.Core;
using MCromwell.StaffIntranet.DTO;
using NUnit.Framework;

namespace MCromwell.StaffIntranet.Task.Test
{
    public abstract class AbstractTaskSpec : Specification
    {
        protected ILog mockLog;
        protected IServiceResolver stubResolver;

        public override void Before_each_spec()
        {
            stubResolver = CreateStub<IServiceResolver>();
            mockLog = CreateMock<ILog>();
                       
            SetupResult.For(stubResolver.Resolve<ILog>())
                        .Return(mockLog);
            Mocks.Replay(stubResolver);
            
            IoC.InitializeWith(stubResolver);
                        
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            SimpleObjectMapperContainer.ClearMappers();
            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Administrator, AdministratorDTO>());
            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Location, LocationDTO>());

            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<Staff, StaffDTO>()
                                                        .Explicit((staff, staffDTO) => staffDTO.LocationId = staff.Location.Id)
                                                        .Explicit((staff, staffDTO) => staffDTO.LocationDescription = staff.Location.Value)
                                                        .Explicit((staff, staffDTO) => staffDTO.Photo.Data = staff.Photo));

            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<StaffDTO, Staff>()
                                                        .Explicit((staffDTO, staff) =>
                                                        {
                                                            if ((staffDTO.Photo.SaveOption == PhotoSaveOption.Update) && (staffDTO.Photo.Data != null))
                                                                staff.Photo = staffDTO.Photo.Data;
                                                        })
                                                        .Explicit((staffDTO, staff) => staff.Location.Id = staffDTO.LocationId));

            SimpleObjectMapperContainer.RegisterMapper(new SimpleObjectMapper<DomainObjectValidationResult, NotificationDTO>()
                                                        .Explicit((domain, dto) =>
                                                            {
                                                                foreach (var item in domain.Results)
                                                                    dto.AddError(item.ValidationId, item.Message);
                                                            }));
        }
    }
}
